package test.greedy;

import bean.Task;
import bean.Worker;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

public class MaxItemTest1 {
    public static void main(String[] args) {
        //1. 准备数据
        //工人数据
        HashSet<String> w1Skills = new HashSet<>();
        w1Skills.add("e1");
        w1Skills.add("e2");
        Worker w1 = new Worker("w1",w1Skills, 2.0,1);

        HashSet<String> w2Skills = new HashSet<>();
        w2Skills.add("e1");
        Worker w2 = new Worker("w2",w2Skills, 1.0,1);

        HashSet<String> w3Skills = new HashSet<>();
        w3Skills.add("e2");
        w3Skills.add("e3");
        Worker w3 = new Worker("w3",w3Skills, 3.0,2);

        HashSet<Worker> workers = new HashSet<>();
        workers.add(w1);
        workers.add(w2);
        workers.add(w3);

        //任务数据
        HashSet<String> taskSkills = new HashSet<>();
        taskSkills.add("e1");
        taskSkills.add("e2");
        taskSkills.add("e3");
        Task task = new Task(taskSkills);

        //team输出
        HashSet<Worker> team = new HashSet<>();
        //a. 假设此时team中没人

        //b. 假设此时team中有worker1
        //team.add(w1);

        //c. 假设此时team中有worker1,2
        team.add(w1);
        team.add(w2);

        //2. 测试maxItem代码
        int i = 0;
        i = maxItem(team,task.getSkills());
        System.out.println(i);


    }

    /**
     * Top1贪心算法 的第三行的 MAXITEM(g)
     * 用来计算当前团队中工人技能集合与任务技能集合的交集
     * Attention！！！要考虑到工人的 capacity
     * @param team 当前组建的团队
     * @param taskSkills 任务技能集合
     * @return 返回交集中技能的数量
     */
    public static int maxItem(HashSet<Worker> team, HashSet<String> taskSkills) {
        //此集合用来存放，team中工人的技能集合与任务技能集合的交集
        HashSet<String> intersection = new HashSet<>();
        if (team.size()==0){
            return 0;
        }
        //遍历team中的每一个工人
        for (Worker w : team) {
            ArrayList<String> wSkills = new ArrayList<>();
            Iterator<String> iterator = w.getSkills().iterator();
            while (iterator.hasNext()) {
                wSkills.add(iterator.next());
            }
            //count用来标记工人的能力，如果工人技能中是任务所需的技能数量大于工人能力，则不再加入计算
            int capacity = w.getCapacity();
            int count = 0;
            //比较当前工人的技能和任务所需技能，如果工人技能是任务所需的，则将此技能加入交集intersection
            f : for (String wSkill : wSkills) {
                for (String tSkill : taskSkills) {
                    if (wSkill == tSkill) {
                        if (count==capacity){
                            break f;
                        }
                        count++;
                        intersection.add(wSkill);
                    }
                }
            }
        }
        return intersection.size();
    }
}
